package com.lianniu.mgr.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;






import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.dao.IProductSpecDao;
import com.lianniu.mgr.product.model.ProductSpecVo;
import com.lianniu.mgr.product.service.IProductSpecService;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ProductSpecServiceImpl implements IProductSpecService {
	/**
	 * 
	*/
	private final static Logger logger = Logger.getLogger(ProductSpecServiceImpl.class);
	
	@Resource
	IProductSpecDao productSpecDao;

	@Override
	public BaseResult insertProductSpec(ProductSpecVo psVo) {
	    logger.info("insertProductSpec begin");
	    return BaseResult.success(productSpecDao.insertProductSpec(psVo));
	}

	@Override
	public BaseResult selectByProductSpecForPage(ProductSpecVo psVo,
			PaginationInfo paginationInfo) {
		logger.info("selectByProductSpecForPage begin.");
	    if (!StringUtil.isEmpty(psVo.getSpecName())) {
	    	psVo.setSpecName(psVo.getSpecName().replace(" ", ""));
	    }
	    logger.info("selectByProductSpecForPage specName:" +psVo.getSpecName());
	    if (!StringUtil.isEmpty(psVo.getSpecId())) {
	    	psVo.setSpecId(psVo.getCateId().replace(" ", ""));
	    }
	    logger.info("selectByProductSpecForPage SpecId:" +psVo.getSpecId());
	    logger.info("selectByProductSpecForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    psVo.setPaginationInfo(paginationInfo);
	    List<ProductSpecVo> list=productSpecDao.selectByProductSpecForPage(psVo);
	    Map<String,Object> map=new HashMap<String,Object>();
	    map.put("total", paginationInfo.getTotalRecord());
	    map.put("curPage", paginationInfo.getCurPage());
	    map.put("rows", list);
	    logger.info("selectByProductSpecForPage:  end");    
	    
	    return BaseResult.success(map); 
	}

	@Override
	public BaseResult updateById(ProductSpecVo psVo) {
		logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById ProductSpecVo:" +JSON.toJSONString(psVo));	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(psVo.getSpecName())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(psVo.getSortNum())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	   
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(productSpecDao.updateById(psVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
		logger.info("deleteById begin");
		 logger.info("deleteById Id:" +Id);
		  
		 return BaseResult.success(productSpecDao.deleteById(Id));
	}

	@Override
	public BaseResult selectById(ProductSpecVo psVo) {
		 logger.info("selectById begin");
		    logger.info("selectById Id:" +psVo.getSpecId());
		  
		    return BaseResult.success(productSpecDao.selectById(psVo.getSpecId()));
	}

	@Override
	public Boolean checkSpecName(String specName) {
		 logger.info("checkSpecName begin");
		    logger.info("checkSpecName specName:" +specName.toString());
		    logger.info("checkSpecName: input data check begin");   
		  
		    if (StringUtil.isEmpty(specName)) {       
		      return false;
		    }
		    logger.info("checkSpecName: input data check end");
		    
		    ProductSpecVo ps=new ProductSpecVo();	   
		    ps.setSpecName(specName);
		    int count=productSpecDao.count(ps);
		    if(count>0){
		      return false;      
		    }
		    else{
		      return true;
		    }   
	}

	@Override
	public BaseResult deleteProductSpecBatch(String... id) {
		 logger.info("deleteProductSpecBatch begin");
		    logger.info("deleteProductSpecBatch Ids:" +id.toString());
		    logger.info("deleteProductSpecBatch: input data check begin");
		   
		    BaseResult baseResult;
		    if (StringUtil.isEmpty(id.toString())) {
		      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
		    logger.info("deleteProductSpecBatch: input data check end");
		    
		    return BaseResult.success(productSpecDao.deleteProductSpecBatch(id));
	}

	@Override
	public BaseResult selectParentSpec(String cateId) {
		logger.info("selectParentSpec begin");
	    logger.info("selectParentSpec parm: null");
	  
	    return BaseResult.success(productSpecDao.getProductCateKeyValue(cateId));
	}
	/**
	 * 
	 */
	@Override
	public BaseResult insertProductSpecs(List<ProductSpecVo> list) {
		return BaseResult.success(productSpecDao.insertProductSpecs(list));
	}
	/**
	 * 
	 */
	@Override
	public BaseResult selectByCateId(String cateId) {
		logger.info("selectByCateId begin");
		logger.info("selectByCateId cateId="+cateId);
		return BaseResult.success(productSpecDao.selectByCateId(cateId));
	}
	@Override
	public BaseResult selectByProductCateId(String cateId) {
		logger.info("selectByProductCateId begin");
		logger.info("selectByProductCateId cateId:"+cateId);
		return BaseResult.success(productSpecDao.selectByProductCateId(cateId));
	}

	@Override
	public BaseResult selectSpecNameByCateId(String cateId) {
		logger.info("selectSpecNameByCateId begin");
		return BaseResult.success(productSpecDao.selectSpecNameByCateId(cateId));
		
	}

}
